package com.javalab.recorder;

import java.util.List;
import java.util.Scanner;

import javax.sound.sampled.Mixer;

public class SoundRecorderUI {
	private Scanner input = new Scanner(System.in);

	public Mixer.Info getSelectedMixerInfo(List<Mixer.Info> mixerInfoList) {
		printMixerSelectList(mixerInfoList);
		int selectedMixerInfoIndex = getUserInputInteger();
		if (selectedMixerInfoIndex != -1 && selectedMixerInfoIndex < mixerInfoList.size()) {
			return mixerInfoList.get(selectedMixerInfoIndex);
		}
		return null;
	}

	public void waitingForStartingRecording() {
		System.out.println("Press <Enter> to start recording");
		getUserInputEnter();
	}

	public void waitingForStopingRecording() {
		System.out.println("Press <Enter> to stop recording");
		getUserInputEnter();
	}

	private void printMixerSelectList(List<Mixer.Info> mixerInfoList) {
		for (int i=0;i<mixerInfoList.size();i++) {
			System.out.format("%d: %s\n", i, mixerInfoList.get(i).getName());
		}
	}

	private int getUserInputInteger() {
		String inputString = input.nextLine();
		int returnValue = -1;
		try {
			returnValue = Integer.parseInt(inputString);
		} catch (NumberFormatException ex) {
			ex.printStackTrace();
		}
		return returnValue;
	}

	private void getUserInputEnter() {
		input.nextLine();
	}
}
